package com.dpp.mapper;

import com.dpp.entity.Coupon;
import com.dpp.entity.CouponBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CouponBookMapper {

    @Insert("insert into coupon_book(openid, coupon_id, count) values (#{openid},#{couponId},#{count}) ")
    void save(CouponBook couponBook);

    @Select("select  t1.name, t1.condition, t1.start_at, t1.end_at, t1.description, t1.value, t1.value_desc, t1.unit_desc, t1.custom_valid_period, t1.reason,t1.id as coupon_id,t2.count from coupon t1,coupon_book t2 where t1.id = t2.coupon_id and t2.openid = #{openid} and #{time}>=t1.start_at and #{time} <= t1.end_at")
    List<Coupon> selectCoupon(String openid, long time);

    @Select("select t1.name, t1.condition, t1.start_at, t1.end_at, t1.description, t1.value, t1.value_desc, t1.unit_desc, t1.custom_valid_period, t1.reason,t1.id as coupon_id,t2.count from coupon t1,coupon_book t2 where t1.id = t2.coupon_id and t2.openid = #{openid} and (#{time} < t1.start_at or #{time} > t1.end_at)")
    List<Coupon> selectDisCoupon(String openid, long time);

    @Select("select * from coupon_book where openid=#{openid}")
    List<CouponBook>  selectListByOpenid(String openid);
    @Select("select * from coupon_book where openid=#{openid} and coupon_id =#{id}")
    CouponBook  selectByOpenidByCouponId(String openid,Integer id);
    @Update("update coupon_book SET count=count-1 WHERE openid=#{openid} and coupon_id =#{couponId}" )
    void updateReduceCount(String openid, Integer couponId);
    @Update("update coupon_book SET count=count+1 WHERE openid=#{openid} and coupon_id =#{couponId}" )
    void updateAddCount(String openid, Integer couponId);
    @Update("update coupon_book SET count=count+3 WHERE openid=#{openid} and coupon_id =#{couponId}" )
    void updateAddCount3(String openid, Integer couponId);
}

